<template>
  <el-dialog :title="info.title" :visible.sync="info.isShow" @opened="opened">
    <el-form :model="form">
      <el-form-item label="标题" :label-width="formLabelWidth">
        <el-input v-model="form.title" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-upload
          multiple
          class="avatar-uploader"
          action="#"
          :show-file-list="false"
          :on-change="submit"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="form.status"
          active-color="green"
          inactive-color="grey"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button v-if="info.isAdd" type="primary" @click="comfirm">确 定</el-button>
      <el-button v-else type="primary" @click="update">修 改</el-button>
    </span>
  </el-dialog>
</template>


<script>
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["info"],
  data() {
    return {
      imageUrl: "",
      formLabelWidth: "120px"
    };
  },
  methods: {
    ...mapActions({
      addbanner: "banner/addActions",
      clearbanner: "banner/clearActions",
      requestone: "banner/oneActions",
      updatebanner: "banner/updateActions"
    }),
    opened() {
      // console.log(this.form.img);
      this.imageUrl = this.$pregImg + `${this.form.img}`;
      // console.log(this.imageUrl);
    },
    submit(file) {
      //图片上传
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件大小不能超过2M!!!");
        return;
      }
      let ext = [".png", ".jpeg", ".jpg", ".gif", ".webp"];
      let extName = file.name.slice(file.name.lastIndexOf("."));
      if (!ext.some(item => item === extName)) {
        errorAlert("文件格式不正确,请重新上传");
        return;
      }
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.img = file.raw;
    },
    cancel() {
      this.info.isShow = false;
      this.info.isAdd = true;
      this.info.title = "添加";
      this.clearbanner();
      this.imageUrl = "";
    },
    update() {
      this.updatebanner(this.form);
      this.cancel();
    },
    comfirm() {
      this.addbanner(this.form);
      this.cancel();
    },
    getone(id) {
      this.requestone(id);
    }
  },
  computed: {
    ...mapGetters({
      form: "banner/form"
    })
  }
};
</script>

<style scoped>
.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader >>> .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>